/**
 *检测点击的doom 节点是否在某节点外面
 */
import { ref, onMounted, onUnmounted } from 'vue';

const useClickOutside = (elementRef) => {
  const isClickOutside = ref(false);

  const handler = (e) => {
    if (elementRef.value) {
      //   点击的dom节点不是该node的子节点
      if (elementRef.value.contains(e.target)) {
        isClickOutside.value = false;
      } else {
        isClickOutside.value = true;
      }
    }
  };

  onMounted(() => {
    document.addEventListener('click', handler);
  });

  onUnmounted(() => {
    document.removeEventListener('click', handler);
  });

  return isClickOutside;
};

export default useClickOutside;
